package org.abstractmeta.commandify.generator.util;

import org.abstractmeta.code.g.code.JavaType;
import org.abstractmeta.code.g.core.util.JavaTypeUtil;
import org.abstractmeta.code.g.core.util.ReflectUtil;
import org.abstractmeta.commandify.Commandify;

import java.lang.reflect.Type;
import java.util.Iterator;

/**
 * Represents CommandifyGeneratorUtil
 * <p>
 * </p>
 *
 * @author Adrian Witas
 */
public class CommandifyGeneratorUtil {

    public static Type getSuperType(JavaType owner) {
        boolean superTypeBased = JavaTypeUtil.hasSuperTypeDefined(owner);
        if(superTypeBased ) {
            return owner.getSuperType();
        }
        if(! owner.getSuperInterfaces().isEmpty()) {
            Iterator<Type> iterator = owner.getSuperInterfaces().iterator();
            while (iterator.hasNext()) {
                Type candidate = iterator.next();
                Class rawType = ReflectUtil.getRawClass(candidate);
                if(Commandify.class.isAssignableFrom(rawType))  continue;
                return candidate;
            }

        }
        return Object.class;
    }
}
